package com.datas.service;

import com.datas.db.hibernatebeans.UmsRegionCity;
import com.datas.db.hibernatebeans.UmsRegionProvince;
import com.datas.db.hibernatebeans.UmsRegionTown;
import com.datas.utils.hbn.HbnTxRuner;
import org.hibernate.Session;
import org.json.JSONArray;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by jige0727 on 2017/5/30.
 */
@Service
public class InitProvinceService {
    public void initTown() {
        new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                initTown(getCurrentSession());
                return 1;
            }
        }.runTx();
    }

    private void initTown(Session session) {
        JSONArray datas = new JSONArray(readFileByLines("D:\\gitall-tst\\zhongliang\\plan2\\ums\\src\\main\\resources\\townData.json"));
        Integer provinceOrder = 0, cityOrder = 0, townOrder = 0;
        Map<String, Integer> keys = new HashMap<>();
        for (int i = 0; i < datas.length(); i++) {
            JSONArray data = datas.getJSONArray(i);
//            LoggerFactory.getLogger(getClass()).info("data -> " + data);
//            session.flush();

            String countryCode = data.getString(0);
            countryCode = countryCode.equals("x") ? "" : countryCode;
            String SAPCode = data.getString(1);
            SAPCode = SAPCode.equals("x") ? "" : SAPCode;
            String provinceCode = data.getString(2);
            String provinceName = data.getString(3);
            String cityCode = data.getString(4);
            String cityName = data.getString(5);
            String townCode = data.getString(6);
            String townName = data.getString(7);
            if (!keys.containsKey(provinceCode)) {
                keys.put(provinceCode, 1);
                UmsRegionProvince province = new UmsRegionProvince(provinceName, provinceCode, ++provinceOrder, countryCode, SAPCode);
                session.save(province);
            }
            if (!keys.containsKey(cityCode)) {
                keys.put(cityCode, 1);
                UmsRegionCity province = new UmsRegionCity(cityName, cityCode, ++cityOrder, countryCode, provinceCode);
                session.save(province);
            }
            if (!keys.containsKey(townCode)) {
                keys.put(townCode, 1);
                UmsRegionTown province = new UmsRegionTown(townName, townCode, ++townOrder, countryCode, provinceCode, cityCode);
                session.save(province);
            }
        }
    }

    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */
    private String readFileByLines(String fileName) {
        File file = new File(fileName);
        BufferedReader reader = null;
        StringBuffer sb = new StringBuffer();
        try {
//            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                // 显示行号
                sb.append(tempString);
            }
            reader.close();
        } catch (Exception e) {
            LoggerFactory.getLogger(getClass()).error("error -> ", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e1) {
                }
            }
        }
        return sb.toString();
    }
}
